<?php
include_once('./config/config.php');
// 校验用户是否登陆
include_once('./config/check.php');

if ($_POST)
{
    $adminid = isset($_POST['adminid']) ? trim($_POST['adminid']) : 0;

    $sql = "SELECT *  FROM per_department WHERE `id` = $adminid";

    $dep = find($sql);

    if (!$dep)
    {
        ShowMsg('部门不存在', 'depList.php');
        exit;
    }

    $result = del('department', "`id` = $adminid");

    if (!$result)
    {
        ShowMsg('删除失败');
        exit;
    }


    ShowMsg('删除成功');
    exit;
}

$page = $_GET['page'] ?? 1;
$limit = 2;
// 偏移量
$start = ($page - 1) * $limit;
// 显示页码
$size = 3;
// 总数量
$countsql = "SELECT COUNT(*) as count FROM per_admin";
// 查询总数量
$total = find($countsql);

// 分页功能
$pagestr = page($page, $total['count'], $limit, $size);

$sql = "SELECT * FROM `per_department` ORDER BY `id` DESC LIMIT $start,$limit";
$adminlist = all($sql);

?>
<!DOCTYPE html>
<html lang="en">

<head>
    <?php include_once('common/meta.php'); ?>
    <!-- 引入分页样式 -->
    <link rel="stylesheet" href="./assets/page/css.css">
</head>

<body>


    <?php include_once('common/header.php'); ?>

    <?php include_once('common/menu.php'); ?>

    <div class="content">
        <div class="header">
            <h1 class="page-title">部门列表</h1>
        </div>
        <ul class="breadcrumb">
            <li><a href="index.php">Home</a> <span class="divider">/</span></li>
            <li class="active">Index</li>
        </ul>

        <div class="container-fluid">
            <div class="row-fluid">
                <div class="btn-toolbar">
                    <button class="btn btn-primary" onClick="location='depAdd.php'"><i class="icon-plus"></i>部门</button>
                </div>
                <div class="well">
                    <table class="table">
                        <thead>
                            <tr>
                                <th>ID</th>
                                <th>部门名</th>
                                <th style="width: 40px;">操作</th>
                            </tr>
                        </thead>
                        <tbody>
                            <?php foreach ($adminlist as $item)
                            { ?>
                                <tr>
                                    <td><?php echo $item['id'] ?></td>
                                    <td><?php echo $item['name'] ?></td>
                                    
                                    <td>
                                        <a href="depUpfate.php?depid=<?php echo $item['id'] ?>"><i class="icon-pencil"></i></a>
                                        <a href="#myModal" role="button" data-toggle="modal"><i class="icon-remove" onclick="del(<?php echo $item['id'] ?>)"></i></a>
                                    </td>
                                </tr>
                            <?php } ?>
                        </tbody>
                    </table>
                </div>

                <div class="pagination">
                    <?php echo $pagestr; ?>
                </div>
                <form method="post">
                    <!-- 隐藏域 -->
                    <input type="hidden" name="adminid" id="adminid">
                    <div class="modal small hide fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                        <div class="modal-header">
                            <button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
                            <h3 id="myModalLabel">删除确认框</h3>
                        </div>
                        <div class="modal-body">
                            <p class="error-text"><i class="icon-warning-sign modal-icon"></i>确认是否删除当前部门?</p>
                        </div>
                        <div class="modal-footer">
                            <button class="btn" data-dismiss="modal" aria-hidden="true">取消</button>
                            <button type="submit" class="btn btn-danger">确定</button>
                        </div>
                    </div>
                </form>

                <?php include_once('common/footer.php'); ?>

            </div>
        </div>
    </div>
</body>

</html>
<?php include_once('common/script.php'); ?>
<script>
    function del(id) {
        $("#adminid").val(id)
    }
</script>